﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using log4net;

namespace BarberShop
{
    public partial class AddOperatoinForm : Form
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof (AddOperatoinForm));

        private readonly dbDataSet.ServicesDataTable _services;

        public AddOperatoinForm()
        {
            InitializeComponent();
        }

        public AddOperatoinForm(dbDataSet.ServicesDataTable services)
        {
            InitializeComponent();

            _services = services;
            serviceComboBox.DataSource = _services;
            serviceComboBox.DisplayMember = _services.NameColumn.ColumnName;
            serviceComboBox.ValueMember = _services.ServiceIdColumn.ColumnName;
        }

        public int ServiceId { get; set; }
        public double Price { get; set; }
        public DateTime Date { get; set; }


        private void AddOperatoinFormLoad(object sender, EventArgs e)
        {
            dateTimePicker.MinDate = DateTime.Now.AddYears(-1);
            dateTimePicker.Value = DateTime.Now;
            dateTimePicker.MaxDate = DateTime.Now.AddYears(1);
        }

        private void AddButtonClick(object sender, EventArgs e)
        {
            AddOperation();
        }

        private void AddOperation()
        {
            if (serviceComboBox.SelectedValue == null)
            {
                pricaErrorProvider.SetError(serviceComboBox, "Тип послуги не можу бути порожнім");
                return;
            }

            int selectedServiceId = int.Parse(serviceComboBox.SelectedValue.ToString());
            dbDataSet.ServicesRow row = _services.FindByServiceId(selectedServiceId);

            if (ValidatePriceIsNumber())
            {
                if (row.Billable == "T")
                {
                    if (ValidatePriceIsEmpty())
                    {
                        Price = double.Parse(priceTextBox.Text);
                        ServiceId = selectedServiceId;
                        Date = dateTimePicker.Value;

                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
                else
                {
                    try
                    {
                        Price = double.Parse(priceTextBox.Text);
                    }
                    catch (Exception e)
                    {
                        Log.Warn("Unable to parse price " + priceTextBox.Text, e);
                    }

                    ServiceId = selectedServiceId;
                    Date = dateTimePicker.Value;

                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }

        private bool ValidatePriceIsNumber()
        {
            if (priceTextBox.Text.Trim() == "")
            {
                return true;
            }
            double result;
            bool isNumber = double.TryParse(priceTextBox.Text, out result);
            Price = result;
            if (!isNumber)
            {
                pricaErrorProvider.SetError(priceTextBox, "Введене значення не є числом");
            }
            return isNumber;
        }

        private bool ValidatePriceIsEmpty()
        {
            if (priceTextBox.Text.Trim() == "")
            {
                pricaErrorProvider.SetError(priceTextBox, "Вартість не може бути порожньою");
                return false;
            }
            return true;
        }

        private void PriceTextBoxValidating(object sender, CancelEventArgs e)
        {
            ValidatePriceIsNumber();
        }

        private void CloseButtonLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void FilterPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 13: //Enter key
                    AddOperation();
                    break;
                case 27: //Esc key
                    Close();
                    break;
            }
        }

        private void ServiceComboBoxPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 27: //Esc key
                    Close();
                    break;
            }
        }
    }
}